<template>
  <div class="app-container">
    <div class="tech-border-out">
      <div class="tech-border">
        <el-card>
        <div slot="header" class="clearfix">
          <dv-decoration-7 style="width:150px;height:30px;;"> <p style="padding: 0 5px;">权限管理</p>  </dv-decoration-7>
        </div>

        <div class="filter-container">


          <el-button v-waves :disabled="!checkPermission(['rule.create'])" class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
            {{ '添加' }}
          </el-button>
        </div>

        <el-table
        
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          :data="list"
          size="medium"
          fit
          highlight-current-row
          class="table-container"
          style="width: 100%"
          row-key="id"
          :tree-props="{children: 'children'}"
        >
          <el-table-column min-width="100px" :label="'名称'">
            <template slot-scope="scope">
              <span>{{ scope.row.title }}</span>
            </template>
          </el-table-column>
          <el-table-column min-width="100px" :label="'url'">
            <template slot-scope="scope">
              <span>{{ scope.row.url }}</span>
            </template>
          </el-table-column>
          <el-table-column min-width="100px" :label="'method'">
            <template slot-scope="scope">
              <span>{{ scope.row.method }}</span>
            </template>
          </el-table-column>
          <el-table-column min-width="100px" :label="'slug'">
            <template slot-scope="scope">
              <span>{{ scope.row.slug }}</span>
            </template>
          </el-table-column>

          <el-table-column min-width="150px" align="center" :label="'添加时间'">
            <template slot-scope="scope">
              <span>{{ scope.row.create_time | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" :label="'操作'" min-width="350">
            <template slot-scope="scope">
              <el-button :disabled="!checkPermission(['rule.update'])" type="primary" size="mini" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">
                {{ '编辑' }}
              </el-button>
              <el-button :disabled="!checkPermission(['rule.detail'])" type="info" size="mini" icon="el-icon-info" @click="handleDetail(scope.$index, scope.row)">
                {{ '详情' }}
              </el-button>
              <el-button v-permission="['rule.delete']" type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.$index, scope.row)">
                {{ '删除' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

      <!-- <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" /> -->
      </el-card>
      </div>
    </div>

    
    <el-dialog   width="650px"  :visible.sync="detail.dialogVisible" :show-close="false">
      <dv-border-box-11  class="el-new-dialog" title="权限详情">
        <detail :data="detail.data" />
      </dv-border-box-11>
    </el-dialog>

    <el-dialog   width="650px"  :visible.sync="create.dialogVisible" :show-close="false">
      <dv-border-box-11  class="el-new-dialog" title="添加权限">
        <create :item="create" />
      </dv-border-box-11>
    </el-dialog>

    <el-dialog   width="650px"  :visible.sync="edit.dialogVisible" :show-close="false">
      <dv-border-box-11  class="el-new-dialog" title="编辑权限">
        <edit :item="edit" />
      </dv-border-box-11>
    </el-dialog>


  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import permission from '@/directive/permission/index.js' // 权限判断指令
import checkPermission from '@/utils/permission' // 权限判断函数
import Detail from '@/components/Maidou/Detail'
import Edit from './components/Edit'
import Create from './components/Create'
import { list, detail, del } from '@/api/rule'
import { parseTime } from '@/utils'
export default {
  name: 'RuleIndex',
  components: { Pagination, Detail, Edit, Create },
  directives: { waves, permission },
  filters: {
    parseTime
  },
  data() {
    return {
      list: [],
      total: 0,
      // listLoading: true,
      listQuery: {
        searchword: '',
        page: 1,
        limit: 10
      },
 
      create: {
        dialogVisible: false
      },
      edit: {
        dialogVisible: false,
        id: ''
      },
      detail: {
        dialogVisible: false,
        data: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    getList() {
      // this.listLoading = true
      list(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        // this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.create.dialogVisible = true
    },
    handleEdit(index, row) {
      this.edit.dialogVisible = true
      this.edit.id = row.id
    },
    handleDetail(index, row) {
      detail(row.id).then((res) => {
        this.detail.dialogVisible = true
        const data = res.data
        this.detail.data = [
          {
            name: 'ID',
            content: data.id,
            type: 'text'
          },
          {
            name: '名称',
            content: data.title,
            type: 'text'
          },
          {
            name: 'url',
            content: data.url,
            type: 'text'
          },
          {
            name: 'method',
            content: data.method,
            type: 'text'
          },
          {
            name: 'slug',
            content: data.slug,
            type: 'text'
          },
          {
            name: '简介',
            content: data.description,
            type: 'text'
          },
          {
            name: '加入时间',
            content: data.create_time,
            type: 'time'
          },
          {
            name: '最近活动',
            content: data.update_time,
            type: 'time'
          },
          {
            name: '最近活动IP',
            content: data.update_ip,
            type: 'text'
          }
        ]
      })
    },
    handleDelete(index, row) {
      const thiz = this
      this.$confirm('确认要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.id).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1500,
            onClose() {
              thiz.list.splice(index, 1)
            }
          })
        })
      }).catch(() => {

      })
    }
  }
}
</script>

<style scoped>
.pagination-container {
  padding: 5px 2px;
}
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
